package com.aiyi.education.user.controller;



import com.aiyi.education.entity.KsUserDepartmentWithBLOBs;
import com.aiyi.education.user.model.FindsDepartsChildren;
import com.aiyi.education.user.model.SysDepartTreeModel;
import com.aiyi.education.user.service.IKsUserDepartmentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.annotation.Pass;
import com.education.util.vo.Result;

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户部门 前端控制器
 * </p>
 *
 * @author renyong
 * @since 2020-04-22
 */
@Api(tags="用户部门管理")
@Slf4j
@RestController
@RequestMapping("/user/ks-user-department")
public class KsUserDepartmentController {

    @Autowired
    private IKsUserDepartmentService iKsUserDepartmentService;

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     *
     * @return
     */
    @Pass
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<SysDepartTreeModel>> queryTreeList() {
        Result<List<SysDepartTreeModel>> result = new Result<>();
        try {
            List<SysDepartTreeModel> list = iKsUserDepartmentService.queryTreeList();
            result.setResult(list);
            result.setSuccess(true);
        } catch (Exception e) {
            log.error(e.getMessage(),e);
        }
        return result;
    }


    /**
     *   通过id删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<KsUserDepartmentWithBLOBs> delete(@RequestParam(name="id",required=true) String id) {

        Result<KsUserDepartmentWithBLOBs> result = new Result<KsUserDepartmentWithBLOBs>();
        KsUserDepartmentWithBLOBs sysDepart = iKsUserDepartmentService.getById(id);
        if(sysDepart==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = iKsUserDepartmentService.delete(Integer.parseInt(id));
            if(ok) {
               result.success("删除成功!");
            }
        }
        return result;
    }


    /**
     * 编辑数据 编辑部门的部分数据,并保存到数据库
     *
     * @param sysDepart
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result<KsUserDepartmentWithBLOBs> edit(@RequestBody KsUserDepartmentWithBLOBs sysDepart, HttpServletRequest request) {
        Result<KsUserDepartmentWithBLOBs> result = new Result<KsUserDepartmentWithBLOBs>();
        KsUserDepartmentWithBLOBs sysDepartEntity = iKsUserDepartmentService.getById(sysDepart.getDepartId());
        if (sysDepartEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = iKsUserDepartmentService.updateDepartDataById(sysDepart, "admin");
            if (ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }

    /**
     * 添加部门信息
     * @param dept
     * @param request
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result<KsUserDepartmentWithBLOBs> add(@RequestBody KsUserDepartmentWithBLOBs dept,HttpServletRequest request){
        Result<KsUserDepartmentWithBLOBs> result = new Result<KsUserDepartmentWithBLOBs>();
       boolean ok=iKsUserDepartmentService.saveDepartData(dept, "admin");
            if (ok) {
                result.success("添加成功!");
            }

        return result;
    }


}
